package com.wf.ew.sell.service.impl;

import com.wf.ew.common.JsonResult;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.ArithUtil;
import com.wf.ew.sell.biz.EntryBillBiz;
import com.wf.ew.sell.model.EntryBill;
import com.wf.ew.sell.model.Sell;
import com.wf.ew.sell.model.SellSon;
import com.wf.ew.storage.biz.ProductStorageBiz;
import com.wf.ew.storage.model.ProductStorage;
import com.wf.ew.system.model.User;
import com.wf.ew.sell.biz.EntryBillSonBiz;
import com.wf.ew.sell.model.EntryBillSon;
import com.wf.ew.sell.service.EntryBillSonService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;

/**
 *
 * 数据服务层接口实现类
 * @author : Doman
 * @date : 2019-11-24 00:09:57
 * @version ：1.0
 */
@Service
public class EntryBillSonServiceImpl implements EntryBillSonService {

	@Autowired
	private EntryBillSonBiz entryBillSonBiz;
	@Autowired
	private ProductStorageBiz productStorageBiz;
	@Autowired
	private EntryBillBiz entryBillBiz;

	@Override
	public String list(PageResult<T> page, EntryBillSon entryBillSon, User loginUser) {
		return entryBillSonBiz.list(page,entryBillSon,loginUser);
	}
	
	@Override
	public List<EntryBillSon> list(EntryBillSon entryBillSon, User loginUser) {
		return entryBillSonBiz.list(entryBillSon,loginUser);
	}

	@Override
	public Double calcTotalPrice(EntryBillSon entryBillSon, User loginUser) {
		if(entryBillSon == null && entryBillSon.getEntryBillId().equals("")){
			return 0.0;
		} else {
			double total = 0.0;
			try {
				total = entryBillSonBiz.calcTotalPrice(entryBillSon,loginUser);
			} catch (Exception e){
				e.printStackTrace();
			}
			EntryBill entryBill = new EntryBill();
			entryBill.setId(entryBillSon.getEntryBillId());
			entryBill.setTotal(total);
			entryBillBiz.updateTotal(entryBill, loginUser);
			return total;
		}

	}

	@Override
	public EntryBillSon getById(EntryBillSon entryBillSon) {
		return entryBillSonBiz.getById(entryBillSon);
	}

	@Transactional
	@Override
	public JsonResult add(EntryBillSon entryBillSon, User loginUser) {
		if (entryBillSonBiz.add(entryBillSon, loginUser)) {
			if(entryBillSon.getNum()>=0){
				ProductStorage ps = new ProductStorage();
				ps.setWarehouseName(entryBillSon.getWarehouseName());
				ps.setProductName(entryBillSon.getProductName());
				ps.setSpecification(entryBillSon.getSpecification());
				ps.setLengths(entryBillSon.getLengths());
				ps.setNegative(entryBillSon.getNegative());
				ps.setNum(entryBillSon.getNum());
				ps.setLiWeight(entryBillSon.getLiWeight());
				ps.setLiTotalWeight(Double.valueOf(entryBillSon.getLiTotalWeight()));
				ps.setPrice(entryBillSon.getPrice()+"");
				ps.setModel(entryBillSon.getModel());
				ps.setLocality(entryBillSon.getLocality());
				List<ProductStorage> psList = productStorageBiz.entity(ps, loginUser);
				if (psList != null && psList.size()>0) {
					ProductStorage ps2 = psList.get(0);
					ps2.setNum(entryBillSon.getNum());
					ps2.setLiTotalWeight(Double.valueOf(entryBillSon.getLiTotalWeight()));
					productStorageBiz.updateNum(ps2, loginUser);
				} else {
					productStorageBiz.add(ps, loginUser);
				}
			}
			return JsonResult.ok("添加成功");
		}
		return JsonResult.error("添加失败");
	}

	@Transactional
	@Override
	public JsonResult update(EntryBillSon entryBillSon, User loginUser) {
        EntryBillSon b2 = entryBillSonBiz.getById(entryBillSon);
		if (entryBillSonBiz.update(entryBillSon, loginUser)) {
			if(entryBillSon.getNum()>=0){
				ProductStorage ps = new ProductStorage();
				ps.setWarehouseName(entryBillSon.getWarehouseName());
				ps.setProductName(entryBillSon.getProductName());
				ps.setSpecification(entryBillSon.getSpecification());
				ps.setLengths(entryBillSon.getLengths());
				ps.setNegative(entryBillSon.getNegative());
				List<ProductStorage> psList = productStorageBiz.entity(ps, loginUser);
				if (psList != null && psList.size()>0) {
					ProductStorage ps2 = psList.get(0);
					ps2.setNum(ArithUtil.sub(entryBillSon.getNum(),b2.getNum()));
					ps2.setLiTotalWeight(ArithUtil.sub(Double.valueOf(entryBillSon.getLiTotalWeight()),
							Double.valueOf(b2.getLiTotalWeight())));
					productStorageBiz.updateNum(ps2, loginUser);
				} else {
					productStorageBiz.add(ps, loginUser);
				}
			}
			return JsonResult.ok("修改成功");
		}
		return JsonResult.error("修改失败");
	}

	@Transactional
	@Override
	public JsonResult delete(EntryBillSon entryBillSon, User loginUser) {
        entryBillSon.setId(entryBillSon.getIds());
        EntryBillSon b2 = entryBillSonBiz.getById(entryBillSon);
		if (entryBillSonBiz.delete(entryBillSon, loginUser)) {
            ProductStorage ps = new ProductStorage();
            ps.setWarehouseName(b2.getWarehouseName());
            ps.setProductName(b2.getProductName());
            ps.setSpecification(b2.getSpecification());
            ps.setLengths(b2.getLengths());
			ps.setNegative(entryBillSon.getNegative());
            List<ProductStorage> psList = productStorageBiz.entity(ps, loginUser);
            if (psList != null && psList.size()>0) {
                ProductStorage ps2 = psList.get(0);
                ps2.setNum(ArithUtil.sub(0,b2.getNum()));
                ps2.setLiTotalWeight(ArithUtil.sub(0,
						Double.valueOf(b2.getLiTotalWeight())));
                productStorageBiz.updateNum(ps2, loginUser);
            }
			return JsonResult.ok("删除成功");
		}
		return JsonResult.error("删除失败");
	}

}
